package com.daro.ddlaunch.controller;


import com.daro.ddlaunch.dao.LaunchlogMapper;
import com.daro.ddlaunch.pojo.LaunchLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.daro.ddlaunch.pojo.Parameter;
import com.daro.ddlaunch.pojo.Position;
import com.daro.ddlaunch.utils.Utils;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@ServerEndpoint(value = "/websocket")
@Component
public class WebSocketController {

    public static Log log = LogFactory.getLog(WebSocketController.class);

    private static int onlineCount = 0;
    private static CopyOnWriteArrayList<WebSocketController> webSocketSet = new CopyOnWriteArrayList<WebSocketController>();
    private Session session;

    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        webSocketSet.add(this);//加入set中
        addOnlineCount();
        System.out.println("有新连接加入！当前在线人数为" + getOnlineCount());
    }

    @OnClose
    public void onClose() {
        webSocketSet.remove(this);
        subOnlineCount();
        System.out.println("有一连接关闭！当前在线人数为" + getOnlineCount());
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        try {
            System.out.println("来自客户端的消息：" + message);
            System.out.println("session === >" + session);

            String[] split = message.split("\"");
            String lng = split[1];
            String lat = split[3];
            String temp = split[5];
            String vel = split[7];
            String height = split[9];

            // 传入 地理信息
            Parameter parameter = new Parameter(0, Double.valueOf(lng), Double.valueOf(lat), Double.valueOf(temp), Double.valueOf(vel), Double.valueOf(height));

            // 返回 计算好的位置
            List<Position> resPosition = Utils.getPosition(parameter);

            for (Position position : resPosition) {
                // 群发消息
                for (WebSocketController item : webSocketSet) {

                    StringBuilder stringBuilder = new StringBuilder();
                    StringBuilder append = stringBuilder.append(position.getTime()).append(",").append(position.getLongitude()).append(",").append(position.getLatitude()).append(",").append(position.getHeight()).append(",");
                    String resltsub = append.substring(0, append.length() - 1);

//                    System.out.println(resltsub);
                    log.info(resltsub);
                    try {
                        item.sendMessage(resltsub);
                        // 定时1s 发送一条
                        Thread.sleep(1000);
                    } catch (IOException e) {
                        e.printStackTrace();
                        continue;
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        System.out.println("发生错误！");
        throwable.printStackTrace();
    }

    //   服务端发送
    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }

    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        WebSocketController.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        WebSocketController.onlineCount--;
    }
}
